package com.supermap.dataservice.cloud.user;

import com.supermap.dataservice.util.MD5Util;
import com.supermap.dubbo.api.user.entity.InterNetInfo;
import org.apache.ibatis.annotations.*;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Map;
/**
 * @author aqua
 * @description
 * @date 2018年11月11 下午 4:21
 * @email 1301584033@qq.com
 */
@Mapper
public interface UserCloudMapper {
    /**
     * 注册用户
     * */
    @InsertProvider(type = UserMapperSQL.class,method = "inertUserSQL")
    int  inertUser(@Param("InterNetInfo")InterNetInfo interNetInfo);

    /**
     *查看农资
     * */
    @Select("select  t1.username,t1.address, '供销社' as typeName,t1.mobile,t2.url  from t_user t1\n" +
            "inner join t_suply_point t2\n" +
            "on t1.id=t2.userid\n" +
            "and t1.areaCode like concat(#{areaCode},'%')")
    List<Map<String, Object>> getNongzi(@Param("areaCode") String areaCode);

    /**
     *查看用户loginName是否已使用
     * */
    @Select("select userId from t_user where loginName=#{loginName} and del_flg=0")
    int loginNameIsUse(@Param("loginName") String loginName);

    @Select("select userid from t_user where loginName=#{mobile} and del_flg=0")
    int mobileIsUser(@Param("mobile") String mobile);


    @SelectProvider(type = UserMapperSQL.class,method = "statisticsByAreaCodeAndRangeDate")
    List<Map<String,Object>> statisticsByAreaCodeAndRangeDate(@Param("areaCode")String areaCode , @Param("startDate") String startDate , @Param("endDate") String endDate);


    class UserMapperSQL{
        public String inertUserSQL(Map<String,Object> map){
            InterNetInfo interNetInfo=(InterNetInfo)map.get("InterNetInfo");
            StringBuilder stringBuilder=new StringBuilder();
            StringBuilder title=new StringBuilder();
            StringBuilder val=new StringBuilder();
            stringBuilder.append("insert into t_user");
            if(interNetInfo.getUsername()!=null){
                title.append("username,");
                val.append("'"+interNetInfo.getUsername()+"',");
            }
            if(interNetInfo.getLoginName()!=null){
                title.append("loginName,");
                val.append("'"+interNetInfo.getLoginName()+"',");
            }
            if(interNetInfo.getShowName()!=null){
                title.append("showName,");
                val.append("'"+interNetInfo.getShowName()+"',");
            }
            if(interNetInfo.getPassword()!=null){
                title.append("password,");
                try {
                    val.append("'"+MD5Util.EncoderByMd5(interNetInfo.getPassword())+"',");
                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
            if(interNetInfo.getMobile()!=null){
                title.append("mobile,");
                val.append("'"+interNetInfo.getMobile()+"',");
            }
            if(interNetInfo.getAddress()!=null){
                title.append("address,");
                val.append("'"+interNetInfo.getAddress()+"',");
            }
            if(interNetInfo.getEmail()!=null){
                title.append("Email,");
                val.append("'"+interNetInfo.getEmail()+"',");
            }
            if(interNetInfo.getHeadImage()!=null){
                title.append("HeadImage,");
                val.append("'"+interNetInfo.getHeadImage()+"',");
            }
            if(interNetInfo.getIntroduction()!=null){
                title.append("Introduction,");
                val.append("'"+interNetInfo.getIntroduction()+"',");
            }
            if(interNetInfo.getLatitude()!=null){
                title.append("latitude,");
                val.append(interNetInfo.getLatitude()+",");
            }
            if(interNetInfo.getLongitude()!=null){
                title.append("longitude,");
                val.append(interNetInfo.getLongitude()+",");
            }
            if(interNetInfo.getAreaCode()!=null){
                title.append("areaCode,");
                val.append("'"+interNetInfo.getAreaCode()+"',");
            }
            stringBuilder.append("("+title.substring(0,title.lastIndexOf(","))+") values("+val.substring(0,val.lastIndexOf(","))+")");
            return  stringBuilder.toString();
        }

        public String statisticsByAreaCodeAndRangeDate(String areaCode , String startDate , String endDate)
        {
            StringBuffer sql = new StringBuffer("select if(a.areaname=\"重庆市\",\"科研所\",a.areaname) as areaname ,  count(t1.id) as counts from t_area a \n" +
                    "left join  t_user t1  on t1.areaCode = a.areacode \n" );
            if (areaCode != null && !"".equals(areaCode))
            {
                sql.append(" where a.areaCode like \"").append(areaCode).append("%\"");
            }
            if (startDate != null && !"".equals(startDate) && endDate!= null && !"".equals(endDate))
            {
                sql.append(" and t1.createtime between \"").append(startDate).append("\" and \"").append(endDate).append("\"");
            }
            sql.append("group by a.areaid");

            return  sql.toString();
        }

    }



}
